package com.zpark.it.controller;

import com.zpark.it.param.*;
import com.zpark.it.pojo.Category;
import com.zpark.it.service.ProductService;
import com.zpark.it.utils.R;
import org.bouncycastle.cms.bc.BcKEKRecipientInfoGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Title: Product 商品模块的controller
 * @Author Charles
 * @Package com.zpark.it.controller
 * @Date 2023/7/21 14:56
 */
@RestController
@RequestMapping("/product")
public class ProductController{


    @Autowired
    private ProductService productService;

    @PostMapping("/promo")
    public R promo(@RequestBody  @Validated ProductPromoParam productPromoParam ,BindingResult result){
        if (result.hasErrors()){
            return R.fail("商品数据查询失败");
        }
        return productService.promo(productPromoParam.getCategoryName());
    }

    @PostMapping("hots")

    public  R hots(@RequestBody @Validated ProductHotParam productHotParam, BindingResult result){
        if (result.hasErrors()) {
            return  R.fail("数据查询失败");
        }
        return  productService.hots(productHotParam);
    }


    @PostMapping("category/list")

    public R clist(){
        return productService.clist();
    }


    @PostMapping("bycategory")
    public R byCategory(@RequestBody @Validated ProductIdsParam productIdsParam,BindingResult result){
        if (result.hasErrors()) {
            return R.fail("类别商品查询失败");
        }
        return productService.byCategory(productIdsParam);
    }

    @PostMapping("all")
    public R all(@RequestBody @Validated ProductIdsParam productIdsParam,BindingResult result){
        if (result.hasErrors()) {
            return R.fail("类别商品查询失败");
        }
        return productService.byCategory(productIdsParam);
    }


    @PostMapping("detail")
    public R detail(@RequestBody @Validated ProductIdParam productIdParam,BindingResult result){
        if (result.hasErrors()) {
            return R.fail("商品查询失败");
        }
        return productService.detail(productIdParam.getProductID());
    }

    @PostMapping("pictures")
    public R pictures(@RequestBody @Validated ProductIdParam productIdParam,BindingResult result){
        if (result.hasErrors()) {
            return R.fail("商品图片查询失败");
        }
        return productService.pictures(productIdParam.getProductID());
    }

    @PostMapping("search")
    public  R search(@RequestBody ProductSearchParam productSearchParam){
        return productService.search(productSearchParam);
    }

}
